package com.ms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ms.common.Result;
import com.ms.pojo.Employee;
import com.ms.service.UserManageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Tag(name = "员工管理信息接口")
@RestController
@RequestMapping("/userManage")
public class UserManageController {
    @Autowired
    private UserManageService userManageService;

    /**
     * 获取所有员工
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有员工（分页）")
    public Result<IPage<Employee>> getAllEmployees(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        IPage<Employee> employeePage = userManageService.getAllEmployees(page, size);
        return Result.success(employeePage);
    }

    /**
     * 根据ID查询员工
     */
    @GetMapping("/{empId}")
    @Operation(summary = "根据ID查询员工")
    public Result<Employee> getEmployeeById( @PathVariable Integer empId) {
        Employee employee = userManageService.getEmployeeById(empId);
        if (employee != null) {
            return Result.success(employee);
        }
        return Result.error("员工不存在");
    }

    /**
     * 添加员工
     */
    @PostMapping
    @Operation(summary = "添加新员工")
    public Result<String> addEmployee(@RequestBody Employee employee){
        boolean success = userManageService.addEmployee(employee);
        return success ? Result.success("员工添加成功") : Result.error("员工添加失败");
    }

    /**
     * 更新员工信息
     */
    @PutMapping
    @Operation(summary = "更新员工信息")
    public Result<String> updateEmployee(@RequestBody Employee employee) {
        boolean success = userManageService.updateEmployee(employee);
        return success ? Result.success("员工更新成功") : Result.error("员工更新失败");
    }

    /**
     * 删除员工
     */
    @DeleteMapping("/{empId}")
    @Operation(summary = "删除员工")
    public Result<String> deleteEmployee(@PathVariable Integer empId) {
        boolean success = userManageService.deleteEmployee(empId);
        return success ? Result.success("员工删除成功") : Result.error("员工删除失败");
    }
}
